package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author linjiang
 * @since 2022-07-20
 */

@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {
    @Autowired
    private PlatformPropertyValueService platformPropertyValueService;


    ///**
    // * 性能不佳，假如有一万条 平台属性 name,循环查非常浪费性能
    // * @param baseCategory1
    // * @param baseCategory2
    // * @param baseCategory3
    // * @return
    // */
    //@Override
    //public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long baseCategory1, Long baseCategory2, Long baseCategory3) {
    //   //找到平台属性 name
    //    List<PlatformPropertyKey> platformPropertyKeys = baseMapper.getPlatformPropertyByCategoryId(baseCategory1, baseCategory2, baseCategory3);
    //   //找到平台属性 value
    //    for (PlatformPropertyKey platformPropertyKey : platformPropertyKeys) {
    //      //要拿到平台属性值
    //        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
    //        wrapper.eq("property_key_id",platformPropertyKey.getPropertyKey());
    //        List<PlatformPropertyValue> platformPropertyValues = platformPropertyValueService.list(wrapper);
    //        platformPropertyKey.setPropertyValueList(platformPropertyValues);
    //    }
    //    return platformPropertyKeys;
    //}

    /**
     * 采用  联合查询  一个sql
     * @param baseCategory1
     * @param baseCategory2
     * @param baseCategory3
     * @return
     */
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long baseCategory1, Long baseCategory2, Long baseCategory3) {
        return baseMapper.getPlatformPropertyByCategoryId(baseCategory1, baseCategory2, baseCategory3);
    }

    /**
     * 保存  平台属性
     * @return
     */
    @Transactional
    @Override
    public Boolean savePlatformProperty(PlatformPropertyKey platformPropertyKey) {

        //判断是添加还是修改,因为页面是修改和删除共用一个方法,否则你在修改的时候
        //又会生成一个全新的数据
        if(!StringUtils.isEmpty(platformPropertyKey.getId())){
           // 这里就是修改 有id,   保存平台属性
            baseMapper.updateById(platformPropertyKey);
            //这里直接删掉所有 该平台属性的key,然后在添加，达到修改
            QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
            wrapper.eq("property_key_id",platformPropertyKey.getId());
            platformPropertyValueService.remove(wrapper);
        }else {
            //这里就是添加了   保存平台属性
            baseMapper.insert(platformPropertyKey);
        }
        //保存平台属性 值
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        for (PlatformPropertyValue propertyValue : propertyValueList) {
             propertyValue.setPropertyKeyId(platformPropertyKey.getId());
             //platformPropertyValueService.save(propertyValue);
        }
        platformPropertyValueService.saveBatch(propertyValueList);
        return true;
    }

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }
}
